<template>
	<el-dialog :title="title" v-model="open" append-to-body :close-on-click-modal="false" width="300px">
		<el-form ref="formRef" :model="form" label-width="auto">
			<el-row :gutter="10" class="mb20">
				<el-col>
					<el-form-item label="剩余库存" prop="num_stock">
						<el-input v-model="form.num_stock" disabled />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10" class="mb20">
				<el-col>
					<el-form-item label="库存操作" prop="type">
						<el-radio-group v-model="form.type">
							<el-radio :label="1">加库存</el-radio>
							<el-radio :label="2">减库存</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10">
				<el-col>
					<el-form-item label="库存数量" prop="stock_number">
						<el-input v-model="form.stock_number" placeholder="请输入库存" @input="verifiyNumberIntegerStockNum" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10">
				<el-col>
					<el-form-item label="操作密码" prop="password">
						<el-input v-model="form.password" :type="isShowPassword ? 'text' : 'password'" placeholder="请输入操作密码">
							<template #suffix>
								<i
									class="iconfont el-input__icon login-content-password"
									:class="isShowPassword ? 'icon-yincangmima' : 'icon-xianshimima'"
									@click="isShowPassword = !isShowPassword"
								>
								</i>
							</template>
						</el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button type="primary" :loading="submitLoading" @click="onSubmitForm"> 确 定 </el-button>
				<el-button @click="onCancel">取 消</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script lang="ts">
import { reactive, ref, toRefs } from 'vue';
import { couponApi } from '/@/api/admin';
import { ElMessage } from 'element-plus';
import { verifiyNumberInteger } from '/@/utils/toolsValidate';

const CouponApi = couponApi();

export default {
	setup() {
		const state = reactive({
			title: '库存修改',
			open: false,
			form: {} as any,
			submitLoading: false,
			isShowPassword: false,
		});

		const formRef = ref(null);
		let callback: Function = () => {};

		/********** 点击事件(on开头) **********/
		/** 提交按钮 */
		const onSubmitForm = () => {
			(formRef.value as any)?.validate((valid: boolean) => {
				if (valid) {
					state.submitLoading = true;
					_couponEditStock(state.form);
				}
			});
		};
		// 取消按钮
		const onCancel = () => {
			state.submitLoading = false;
			state.open = false;
			reset();
		};

		/********** 方法事件 **********/
		//打开
		const openDialog = (config: any, fn: Function) => {
			console.log(config);
			reset();
			callback = fn;
			state.open = true;
			state.form = {
				coupon_id: config.row.id,
				num_stock: config.row.num_stock,
				stock_number: 0,
				type: 1,
			};
		};
		// 表单重置
		const reset = () => {
			state.form = {
				coupon_id: '',
				num_stock: '',
				stock_number: 0,
				type: 1,
				password: '',
			};
			(formRef.value as any)?.resetFields();
		};
		// 库存数量
		const verifiyNumberIntegerStockNum = (val: string) => {
			state.form.stock_number = verifiyNumberInteger(val);
		};

		/********** 网络事件 **********/
		// 加减库存
		const _couponEditStock = (_data: object) => {
			CouponApi.couponEditStock(_data)
				.then(() => {
					ElMessage.success('操作成功');
					onCancel();
					callback();
				})
				.catch(() => (state.submitLoading = false));
		};

		return {
			formRef,
			onSubmitForm,
			openDialog,
			onCancel,
			verifiyNumberIntegerStockNum,
			...toRefs(state),
		};
	},
};
</script>

<style lang="scss" scoped></style>
